//
//  ViewControllerInfoView.swift
//  movieInfo
//
//  Created by zhangkai on 2023/5/13.
//

import UIKit
import WebKit
//import YBImageBrowser
class ViewControllerInfoView: UIView {
    var model:Movie?{
        
        didSet{
            guard let infoModel = model else { return  }
            
            
            if let path = infoModel.posterPath {
                
                self.bgIMG.downloadImage(fromPath: path)
                self.img.downloadImage(fromPath: path)
            }
            
            self.nameLabel.text = infoModel.title ?? ""
            self.stattLabel.text = "\(infoModel.voteAverage ?? 0.0)"
            self.timeLabel.text = infoModel.releaseDate ?? ""
            
            let arr = [28,12,16,35,80,99,18,10751,14,36,27,10402,9648,10749,878,10770,53,10752,37]
            let arr1 = ["Action","Adventure","Animation","Comedy","Crime","Documentary","Drama","Family","Fantasy","History","Horror","Music","Mystery","Romance","Science Fiction","TV Movie","Thriller","War","Western"]
            
            var titl = ""
            let arr3 = infoModel.genreIds
            for i in 0..<(arr3?.count ?? 0){
                
                var tit = arr3![i]
                for j in 0..<arr.count{
                    if tit == arr[j]{
                       
                        let tempStr = arr1[j]
                        
                        if titl.count == 0{
                            titl = tempStr
                        }else{
                            titl = titl + "," + tempStr
                        }
               
                    }
                    
                }
            }
    
            
            genres.text = "Genres:" + titl
            
        }
        
    }
    
    var bgIMG =  MIImageView(frame: CGRect(x: 0, y: 0, width: 70, height: 70))
    var img = MIImageView(frame: CGRect(x: 0, y: 0, width: 70, height: 70))
    
    var nameLabel = UILabel()
    var stattImg = UIImageView()
    var stattLabel = UILabel()
    var timeLabel = UILabel()

    var genres = UILabel()
    
    
    var callBlock:(()->Void)?


    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(bgIMG)
        bgIMG.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(200)
        }
        bgIMG.layer.cornerRadius = 0
        bgIMG.backgroundColor = .secondarySystemBackground
        bgIMG.contentMode = .scaleAspectFill
        let tap = UITapGestureRecognizer(target: self, action: #selector(clickTap))
        bgIMG.addGestureRecognizer(tap)
        bgIMG.isUserInteractionEnabled = true
        
        
        
        addSubview(img)
        img.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(15)
            make.width.equalTo(100)
            make.height.equalTo(150)
            make.top.equalTo(bgIMG.snp.bottom).offset(-40)
        }
        img.backgroundColor = .purple
        
        addSubview(nameLabel)
        nameLabel.numberOfLines = 2
        nameLabel.font = UIFont.systemFont(ofSize: 20, weight: .regular)
        nameLabel.textColor = .label
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(img.snp.right).offset(15)
            make.top.equalTo(bgIMG.snp.bottom).offset(12)
            make.right.equalToSuperview().offset(-15)
        }
        nameLabel.text = "名称"
        
        
        stattImg.image = UIImage(named: "xingxing")
        addSubview(stattImg)
        stattImg.snp.makeConstraints { make in
            make.left.equalTo(img.snp.right).offset(15)
            make.top.equalTo(nameLabel.snp.bottom).offset(8)
            make.width.equalTo(24)
            make.height.equalTo(24)
        }
        
        stattLabel.text = "N/A"
        stattLabel.textColor = .darkText
        stattLabel.font = UIFont.boldSystemFont(ofSize: 18)
        addSubview(stattLabel)
        stattLabel.snp.makeConstraints { make in
            make.centerY.equalTo(stattImg)
            make.left.equalTo(stattImg.snp.right)
         
        }
        
        
        addSubview(timeLabel)
        timeLabel.numberOfLines = 2
        timeLabel.font = UIFont.systemFont(ofSize: 16, weight: .regular)
        timeLabel.textColor = .label
        timeLabel.snp.makeConstraints { make in
            make.left.equalTo(img.snp.right).offset(15)
            make.top.equalTo(stattLabel.snp.bottom).offset(12)
//            make.right.equalToSuperview().offset(-15)
        }
        timeLabel.text = "2022-12-12"
        
        
        addSubview(genres)
        genres.numberOfLines = 2
        genres.font = UIFont.systemFont(ofSize: 14, weight: .regular)
        genres.textColor = .label
        genres.snp.makeConstraints { make in
            make.left.equalTo(img.snp.left).offset(0)
            make.top.equalTo(img.snp.bottom).offset(12)
//            make.right.equalToSuperview().offset(-15)
        }
        
        genres.text = "Genres:Action"

    }
    
    
    @objc func clickTap(){
        if let path = self.model?.posterPath {
            
            let url = "https://image.tmdb.org/t/p/w500\(path)"
            // 网络图片
            let data1 = YBIBImageData()
            data1.imageURL = NSURL(string: url) as URL?
            data1.projectiveView = self.bgIMG
            
            let browser = YBImageBrowser()
            browser.dataSourceArray = [data1]
            browser.show()
            
        }
//        self.callBlock?()
        
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


}


class ViewControllerSubInfoView: UIView {

    var infoLabel = UILabel()
    
    var infoView = UITextView()

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        let nameLabel = UILabel()
        nameLabel.text = "   Plot"
        nameLabel.textColor = .black
        nameLabel.backgroundColor = .secondarySystemBackground
        nameLabel.font = UIFont.boldSystemFont(ofSize: 20)
        addSubview(nameLabel)
        nameLabel.snp.makeConstraints { make in
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.top.equalToSuperview().offset(8)
            make.height.equalTo(24)
        }
        

//        infoLabel.text = ""
//        infoLabel.textColor = .label
//        infoLabel.font = UIFont.boldSystemFont(ofSize: 16)
//        infoLabel.numberOfLines = 0
//        addSubview(infoLabel)
//        infoLabel.snp.makeConstraints { make in
//            make.left.equalToSuperview().offset(15)
//            make.right.equalToSuperview().offset(-15)
//            make.top.equalTo(nameLabel.snp.bottom).offset(8)
//        }
//
//        infoLabel.minimumScaleFactor = 0.8
//        infoLabel.lineBreakMode = .byWordWrapping
//
//        infoLabel.adjustsFontForContentSizeCategory = true
//        infoLabel.adjustsFontSizeToFitWidth = true
        
        
        
        infoView.text = ""
        infoView.isEditable = false
        infoView.textColor = .darkText
        infoView.font = UIFont.boldSystemFont(ofSize: 16)
        addSubview(infoView)
        infoView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(15)
            make.right.equalToSuperview().offset(-15)
            make.top.equalTo(nameLabel.snp.bottom).offset(8)
            make.height.equalTo(110)
        }
   
        
        
        
        
     
        
        
    }
    
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


}



class ViewControllerCastInfoView: UIView,UICollectionViewDelegate, UICollectionViewDataSource {

    
    var model:[Cast]?{
        
        didSet{
            guard let infoData = model else { return  }
            
            
            
            DispatchQueue.main.async {
                self.collectionView.reloadData()
            }
            
            
        }
   
    }
    
    private var collectionView: UICollectionView!
    var callBlock:((_ id:Int)->Void)?
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        
        let infoLabel = UILabel()
        infoLabel.text = "   Cast"
        infoLabel.textColor = .label
        infoLabel.backgroundColor = .secondarySystemBackground
        infoLabel.font = UIFont.boldSystemFont(ofSize: 20)
        infoLabel.numberOfLines = 0
        addSubview(infoLabel)
        infoLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(0)
            make.right.equalToSuperview().offset(0)
            make.height.equalTo(22)
        }
        
        
        
        
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.sectionInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
        flowLayout.itemSize = CGSize(width: 100, height: 140)
        flowLayout.scrollDirection = .horizontal
        
        collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: flowLayout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(ViewControllerCastInfoViewCell.self, forCellWithReuseIdentifier: "ViewControllerCastInfoViewCell")
        addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
//            make.edges.equalToSuperview()
            make.top.equalTo(infoLabel.snp.bottom).offset(8)
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(142)
        }
         
        
    }
    
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.model?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ViewControllerCastInfoViewCell", for: indexPath) as? ViewControllerCastInfoViewCell ?? ViewControllerCastInfoViewCell()
        
//        cell.backgroundColor = .darkText
        
//        profilePath
        if let path = self.model![indexPath.row].profilePath {
            cell.img.downloadImage(fromPath: path)
        }
        cell.nameLabel.text = self.model![indexPath.row].name
        cell.tipLabel.text = self.model![indexPath.row].character
        
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        
        
        callBlock?(self.model![indexPath.row].id)
        
    }
    
}


class ViewControllerMoviesInfoView: UIView,UICollectionViewDelegate, UICollectionViewDataSource {
    
    
    var callBlock:((_ model:Movie)->Void)?
    
    var model:[Movie]?{
        
        didSet{
            guard let infoData = model else { return  }
            
            
            DispatchQueue.main.async {
                self.collectionView.reloadData()
            }
            
        }
   
    }
    
    private var collectionView: UICollectionView!
    var infoLabel = UILabel()

    override init(frame: CGRect) {
        super.init(frame: frame)
        
  
        infoLabel.text = "   Similar Movies"
        infoLabel.textColor = .label
        infoLabel.font = UIFont.boldSystemFont(ofSize: 20)
        infoLabel.numberOfLines = 0
        infoLabel.backgroundColor = .secondarySystemBackground
        addSubview(infoLabel)
        infoLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(0)
            make.right.equalToSuperview().offset(0)
            make.height.equalTo(22)
        }
        
        
        
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.sectionInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
        flowLayout.itemSize = CGSize(width: 100, height: 140)
        flowLayout.scrollDirection = .horizontal
        
        collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: flowLayout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(ViewControllerMoviesInfoViewCell.self, forCellWithReuseIdentifier: "ViewControllerMoviesInfoViewCell")
        addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
//            make.edges.equalToSuperview()
            make.top.equalTo(infoLabel.snp.bottom).offset(8)
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(142)
        }
         
    }
    
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.model?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ViewControllerMoviesInfoViewCell", for: indexPath) as? ViewControllerMoviesInfoViewCell ?? ViewControllerMoviesInfoViewCell()
        
        if let path = self.model![indexPath.row].posterPath {
            cell.img.downloadImage(fromPath: path)
        }
        

        
        return cell
    }

    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        callBlock?(self.model![indexPath.row])
    }
    
    
    
}


class ViewControllerPreInfoView: UIView,UICollectionViewDelegate, UICollectionViewDataSource {
    
    var preData:VideoData?{
        didSet{
            guard let infoData = preData else { return  }
            
            
            
            DispatchQueue.main.async {
                self.collectionView.reloadData()
            }
          
            
        }
    }

    private var collectionView: UICollectionView!

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        let infoLabel = UILabel()
        infoLabel.text = "   Preview"
        infoLabel.textColor = .label
        infoLabel.font = UIFont.boldSystemFont(ofSize: 20)
        infoLabel.numberOfLines = 0
        infoLabel.backgroundColor = .secondarySystemBackground
        addSubview(infoLabel)
        infoLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(0)
            make.right.equalToSuperview().offset(0)
            make.height.equalTo(22)
        }
        
        
        
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.sectionInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
        flowLayout.itemSize = CGSize(width: 160, height: 120)
        flowLayout.scrollDirection = .horizontal
        
        collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: flowLayout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(ViewControllerPreInfoViewCell.self, forCellWithReuseIdentifier: "ViewControllerPreInfoViewCell")
        addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
//            make.edges.equalToSuperview()
            make.top.equalTo(infoLabel.snp.bottom).offset(8)
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(120)
        }
    }
    
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.preData?.results?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ViewControllerPreInfoViewCell", for: indexPath) as? ViewControllerPreInfoViewCell ?? ViewControllerPreInfoViewCell()
        
//        cell.backgroundColor = .darkText
        
        
        if let path = self.preData!.results![indexPath.row].key {
            
            if let url = URL(string: "https://www.youtube.com/embed/\(path)"){
                cell.movieImage.load(URLRequest(url: url))
            }
      
        }

        return cell
    }

}


class ViewControllerMoviesInfoViewCell:UICollectionViewCell{
    var img = MIImageView(frame: CGRect(x: 0, y: 0, width: 100, height: 140))
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.addSubview(img)
        img.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.equalTo(100)
            make.height.equalTo(140)
        }

        img.contentMode = .scaleAspectFill
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}


class ViewControllerCastInfoViewCell:UICollectionViewCell{
    var img = MIImageView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
    var nameLabel = UILabel()
    var tipLabel = UILabel()
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.addSubview(img)
        img.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.equalTo(100)
            make.height.equalTo(100)
        }

        img.contentMode = .scaleAspectFill
        
        
        nameLabel.textAlignment = .center
        nameLabel.textColor = .label
        nameLabel.font = UIFont.systemFont(ofSize: 14)
        contentView.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { make in
            make.width.equalToSuperview()
            make.left.equalToSuperview()
            make.top.equalTo(img.snp.bottom).offset(5)
        }
        
        tipLabel.textAlignment = .center
        tipLabel.textColor = .secondaryLabel
        tipLabel.font = UIFont.systemFont(ofSize: 12)
        contentView.addSubview(tipLabel)
        tipLabel.snp.makeConstraints { make in
            make.width.equalToSuperview()
            make.left.equalToSuperview()
            make.top.equalTo(nameLabel.snp.bottom).offset(5)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}


class ViewControllerPreInfoViewCell:UICollectionViewCell{
    
    
    var movieImage: WKWebView!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        movieImage = WKWebView()
        addSubview(movieImage)
        movieImage.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
